using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace ShootThemUp.Sources.PlayerInputs.WindowsPhone
{
    /// <summary>
    /// This class determines the player actions and movements in function of the windows phone statement.
    /// The movements are determined by the touchscreen.
    /// </summary>
    public class TouchScreenInput : WindowsPhonePlayerInput
    {

        #region Fields

        /// <summary>
        /// This value represents the previous x touch position on the screen. It's null if there are any touch between two update.
        /// </summary>
        private float? previousX = null;
        /// <summary>
        /// This value represents the previous x touch position on the screen. It's null if there are any touch between two update.
        /// </summary>
        private float? previousY = null;

        #endregion

        #region Constante Fields

        /// <summary>
        /// Player ship max speed. In pixel per ms.
        /// </summary>
        private const float PLAYER_SHIP_MAX_SPEED = 1.5F;
        /// <summary>
        /// This value smooth the movement. Avoid the texture shaking when the position of the ship is near the touch location. In pixel.
        /// </summary>
        private const int SMOOTH_VALUE = 0;
        /// <summary>
        /// This value divide the touch movement. Increase it to reduce the speed movement.
        /// </summary>
        private const int SPEED_REDUCTION = 20;

        #endregion

        #region Speed Method

        /// <summary>
        /// Returns the speeds of the ship in function of the players inputs. It returns the vector between the current position of the ship and the touch location on the touch screen.
        /// </summary>
        /// <param name="playerShipPosition">The player ship position.</param>
        /// <returns>Returns the speeds of the ship in function of the players inputs. It returns the vector between the current position of the ship and the touch location on the touch screen.</returns>
        public Vector2 Speed(Vector2 playerShipPosition, int shipWidth, int shipHeight, float dt)
        {
            Vector2 returnedSpeed = Vector2.Zero;

            // Process touch events
            TouchCollection touchCollection = TouchPanel.GetState();

            if (touchCollection.Count > 0 && touchCollection[0] != null)
            {
                //We are only considering the first touch location, we don't manage the multi touch.
                if (touchCollection[0].State == TouchLocationState.Pressed || touchCollection[0].State == TouchLocationState.Moved)
                {
                    float deltaX = 0;
                    float deltaY = 0;

                    if (this.previousX != null)
                        deltaX = touchCollection[0].Position.X - (float)this.previousX;

                    if (this.previousY != null)
                        deltaY = touchCollection[0].Position.Y - (float)this.previousY;

                    if (Math.Abs(deltaX) < TouchScreenInput.SMOOTH_VALUE)
                        deltaX = 0;

                    if (Math.Abs(deltaY) < TouchScreenInput.SMOOTH_VALUE)
                        deltaY = 0;

                    this.previousX = touchCollection[0].Position.X;
                    this.previousY = touchCollection[0].Position.Y;

                    returnedSpeed = new Vector2(deltaX / dt, deltaY / dt);
                }
            }
            else
            {
                this.previousX = null;
                this.previousY = null;
            }

            return returnedSpeed;
        }

        /// <summary>
        /// This function return 0 because with the touch screen, we have to know the player ship position, to calculate the vector.
        /// </summary>
        /// <returns>
        /// Zero vector, use Speed(Vector2 playerShipPosition) to know the real speed.
        /// </returns>
        public override Vector2 Speed()
        {
            return Vector2.Zero;
        }

        #endregion
    }
}

